<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/6/18 上午11:36
 * +-------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\SysAdmin;
use app\admin\validate\AdminValidate;
use app\common\constants\AdminConstant;
use app\common\controller\AdminController;
use app\Request;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;

/**
 * @ControllerAnnotation(title="管理员")
 */
class Admin extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model         = new SysAdmin();
        $this->validateClass = AdminValidate::class;
        $this->sort          = ['sort' => 'desc', 'id'   => 'desc'];
    }

    /**
     * @NodeAnnotation(title="添加")
     * @param Request $request
     * @return Json
     */
    public function add(Request $request): Json
    {
        if(!$request->isPost()) return $this->errorJson('当前请求不合法！');
        $post = $request->post();
        if(!$this->sceneValidate($post, 'add')) return $this->errorJson($this->msg);
        if (empty($post['password'])) $post['password'] = '123456';
        $post['password'] = password($post['password']);
        try {
            $this->model->transaction(function () use ($post, &$save)
            {
                $save = $this->model->save($post);
            });
        } catch(\Exception $e) {
            return $this->errorJson('新增失败:' . $e->getMessage());
        }
        if(!$save) return $this->errorJson('新增失败');
        return $this->successJson('新增成功');
    }

    /**
     * @NodeAnnotation(title="密码修改")
     * @param Request $request
     * @param         $id
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function password(Request $request,$id): Json
    {
        $row = $this->model->find($id);
        if(empty($row)) return $this->errorJson('数据不存在');
        $post = $request->post();
        if(!$this->sceneValidate($post, 'password')) return $this->errorJson($this->msg);
        try {
            $this->model->transaction(function () use ($post, &$save)
            {
                $save = $this->model->save(['password' => password($post['password'])]);
            });
        }catch (\Exception $e) {
            $this->errorJson('操作失败:'.$e->getMessage());
        }
        if(!$save) return $this->errorJson('操作成功');
        return $this->successJson('操作成功');
    }

    /**
     * @NodeAnnotation(title="修改字段")
     */
    public function modify(Request $request): Json
    {
        if(!$request->isPost()) return $this->errorJson('当前请求不合法！');
        $post = $request->post();
        $row = $this->model->find($post['id']);
        if(!$row) return $this->errorJson('数据不存在');
        if(!$this->sceneValidate($post, 'modify')) return $this->errorJson($this->msg);
        if ($post['id'] == AdminConstant::SUPER_ADMIN_ID && $post['field'] == 'status') {
            $this->errorJson('超级管理员状态不允许修改');
        }
        try {
            $this->model->transaction(function () use ($post, $row) {
                $row->save([
                                   $post['field'] => $post['value'],
                           ]);
            });
        }catch (\Exception $e) {
            return $this->errorJson($e->getMessage());
        }
        return $this->successjson('操作成功');
    }

}